package com.socool.sinofaith.ui;

import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.dao.QuestionAnswerDao;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.BaseMessage;
import com.socool.sinofaith.model.NoticeBean;
import com.socool.sinofaith.model.QuestionAnswerBean;
import com.socool.sinofaith.model.QuestionAnswerReqBean;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;

/**
 * 修改密码
 * 
 * @author maxhsh
 * 
 */
public class ModifyPassActivity extends BaseActivity implements
		CenterViewFactory, SpinnerViewFactory, View.OnClickListener {
	ActionBarHelperBase mActionBarHelperBase;
	private EditText old_pass, new_pass, new_pass2;
	private String str_old_pass, str_new_pass, str_new_pass2;
	private Button submitButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modifypass);
		SocoolApplication.addActivity(this);
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);
		mActionBarHelperBase.setmClickListener(this);

		old_pass = (EditText) findViewById(R.id.old_pass);
		new_pass = (EditText) findViewById(R.id.new_pass);
		new_pass2 = (EditText) findViewById(R.id.new_pass2);
		submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.common, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public View createView() {

		TextView tv = new TextView(this);
		tv.setText("修改密码");
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		return tv;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated
															// method stub
		if (item.getItemId() == R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public View createSpinnerView() {
		ImageView imageview = new ImageView(this);
		return imageview;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.submitButton:
			str_old_pass = old_pass.getText().toString();
			str_new_pass = new_pass.getText().toString();
			str_new_pass2 = new_pass2.getText().toString();
			if (str_old_pass.equals("")) {
				Toast.makeText(this, "请输入原密码", Toast.LENGTH_SHORT).show();
				return;
			}
			if (str_new_pass.equals("")) {
				Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
				return;
			}
			if (str_new_pass2.equals("")) {
				Toast.makeText(this, "请再次输入原密码", Toast.LENGTH_SHORT).show();
				return;
			}
			getData(getSPString(), str_old_pass,
					str_new_pass2);
			break;
		}
	}

	void getData(String _mobile, String _oldPass, String _newPass) {
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.changePwd);
		params.put("did", SinofaithUtil.did);
		params.put("Mobile", _mobile);
		params.put("OldPwd", SinofaithUtil.md5(_oldPass));
		params.put("NewPwd", SinofaithUtil.md5(_newPass));
		fh.get(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {
			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				try {
					BaseMessage baseNoticeBean = HttpJsonAdapter.getInstance()
							.get(t.toString(), BaseMessage.class);
					if (baseNoticeBean.getResult().equals("0")) {
						Toast.makeText(ModifyPassActivity.this,
								baseNoticeBean.getDesc(), Toast.LENGTH_LONG)
								.show();
						ModifyPassActivity.this.finish();
					} else {
						Toast.makeText(ModifyPassActivity.this,
								baseNoticeBean.getDesc(), Toast.LENGTH_LONG)
								.show();
					}
				} catch (Exception e) {
					Toast.makeText(ModifyPassActivity.this, e.getMessage(),
							Toast.LENGTH_LONG).show();
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(ModifyPassActivity.this, strMsg,
						Toast.LENGTH_LONG).show();
			}

		});
	}

	
	private String getSPString(){
		SharedPreferences sp=this.getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}
}
